package com.ph.irp.stock.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.ph.irp.model.PStockOrderdetail;
import com.ph.irp.model.PStockOrder;
import com.ph.irp.stock.dao.StockOrderDao;
import com.ph.irp.stock.dao.StockOrderDetailDao;
import com.ph.util.DBUtil;
import com.ph.util.DateUtil;

public class StockOrderDetailDaoImpl implements StockOrderDetailDao {

	// dataSource is a tool that we use to connect the database
	private ComboPooledDataSource dataSource;

	// setters and getters for instance variables
	public ComboPooledDataSource getDataSource() {
		return dataSource;
	}

	public void setDataSource(ComboPooledDataSource dataSource) {
		this.dataSource = dataSource;
	}

	/**
	 * 新增采购价格
	 */
	@Override
	public void addStockOrderDetail(PStockOrderdetail stockOrderDetail)
			throws Throwable {
		// TODO Auto-generated method stub
		// 数据库连接
		Connection con = null;
		// 预编译语句
		PreparedStatement ps = null;
		// 结果集
		ResultSet rs = null;

		// 构建查询语句
		StringBuilder sql = new StringBuilder();

		sql.append("insert into p_stock_stockorderdetail(StockOrderID,MaterialID,MeasureUnitId,StockAmount,UnitPrice,Money,DutyFrank,TotalUnitPrice,Discount,RealTotalUnitPrice,DiscountPrice,DutyPrice,TotalMoney,DeliveryDate,BasicInstorageAmount,TaxPercent,TaxPrice,InstorageAmount,TicketAmount,BasicTicketAmount,SellOrderCode,Remark)  ");
		
		sql.append("values(") ;
		sql.append(stockOrderDetail.getStockOrderId());
		sql.append(",");
		sql.append(stockOrderDetail.getMaterialId()) ;
		sql.append(",");
		sql.append(stockOrderDetail.getMeasureunitId()) ;
		sql.append(",");
		sql.append(stockOrderDetail.getStockAmount()) ;
		sql.append(",");
		sql.append(stockOrderDetail.getUnitPrice()) ;
		sql.append(",");
		sql.append(stockOrderDetail.getMoney()) ;
		sql.append(",");
		sql.append(stockOrderDetail.getDutyFrank()) ;
		sql.append(",");
		sql.append(stockOrderDetail.getTotalUnitPrice()) ;
		sql.append(",");
		sql.append(stockOrderDetail.getDiscount());
		sql.append(",");
		sql.append(stockOrderDetail.getRealTotalUnitPrice()) ;
		sql.append(",");
		sql.append(stockOrderDetail.getDiscountPrice()) ;
		sql.append(",");
		sql.append(stockOrderDetail.getDutyPrice()) ;
		sql.append(",");
		sql.append(stockOrderDetail.getTotalMoney());
		sql.append(",'") ;
		sql.append(stockOrderDetail.getDeliveryDate()) ;
		sql.append("',") ;
		sql.append(stockOrderDetail.getBasicInstorageAmount()) ;
		sql.append(",") ;
		sql.append(stockOrderDetail.getTaxPercent());
		sql.append(",") ;
		sql.append(stockOrderDetail.getTaxPrice()) ;
		sql.append(",") ;
		sql.append(stockOrderDetail.getInstorageAmount()) ;
		sql.append(",") ;
		sql.append(stockOrderDetail.getTicketAmount()) ;
		sql.append(",") ;
		sql.append(stockOrderDetail.getBasicTicketAmount()) ;
		sql.append(",'");
		sql.append(stockOrderDetail.getSellOrderCode());
		sql.append("','");
		sql.append(stockOrderDetail.getRemark());
		sql.append("')");
		try {
			con = dataSource.getConnection();
			ps = con.prepareStatement(sql.toString());
			ps.execute();

			// ps = con.
		} catch (Exception e) {
			// TODO Auto-generated catch block
			throw new Exception(e.getMessage());
		} finally {
			try {
				DBUtil.setNullObject(con, ps, rs);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				throw e;
			}
		}
	}
	/**
	 * 修改采购订单
	 */
	@Override
	public void deleteStockOrderDetail(String[] ids) throws Throwable {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void editStockOrderDetail(PStockOrderdetail stockOrderDetail)
			throws Throwable {
		// TODO Auto-generated method stub
		
	}
	/**
	 * 根据采购订单查询采购订单明细
	 */
	@Override
	public List<PStockOrderdetail> getStockOrderDetailsByStockOrderId(
			int stockOrderId) throws Throwable {
		// TODO Auto-generated method stub
		//采购订单明细list
		List<PStockOrderdetail> stockOrderDetails = new ArrayList<PStockOrderdetail>();
		
		Connection con = null;
		// 预编译语句
		PreparedStatement ps = null;
		// 结果集
		ResultSet rs = null;

		// 构建查询语句
		StringBuilder sql = new StringBuilder();

		sql.append("select * from p_stock_stockorderdetail where StockOrderID=?");
		
		
		try {
			//得到数据源
			con = dataSource.getConnection();
			ps = con.prepareStatement(sql.toString());
			ps.setInt(1, stockOrderId);
			rs = ps.executeQuery();
			
			while(rs.next()){
				//封装采购订单明细model
				PStockOrderdetail stockOrderDetail = new PStockOrderdetail();
				
				stockOrderDetail.setStockOrderDetailId(rs.getInt("StockOrderDetailID")) ;
				
				stockOrderDetail.setStockOrderId(rs.getInt("StockOrderID")) ;
				
				stockOrderDetail.setMaterialId(rs.getInt("MaterialID")) ;
				
				stockOrderDetail.setMeasureunitId(rs.getInt("MeasureUnitId")) ;
				
				stockOrderDetail.setStockAmount(rs.getInt("StockAmount")) ;
				
				stockOrderDetail.setUnitPrice(rs.getFloat("UnitPrice")) ;
				
				stockOrderDetail.setMoney(rs.getFloat("Money")) ;
				
				stockOrderDetail.setDutyFrank(rs.getFloat("DutyFrank")) ;
				
				stockOrderDetail.setTotalUnitPrice(rs.getFloat("TotalUnitPrice")) ;
				
				stockOrderDetail.setDiscount(rs.getFloat("Discount")) ;
				
				stockOrderDetail.setRealTotalUnitPrice(rs.getFloat("RealTotalUnitPrice")) ;
				
				stockOrderDetail.setDiscountPrice(rs.getFloat("DiscountPrice"));
				
				stockOrderDetail.setDutyPrice(rs.getFloat("DutyPrice"));
				
				stockOrderDetail.setTotalMoney(rs.getFloat("TotalMoney")) ;
				
				stockOrderDetail.setDeliveryDate(DateUtil.date2String(rs.getDate("DeliveryDate"), "yyyy-MM-dd"));
				
				stockOrderDetail.setBasicInstorageAmount(rs.getFloat("BasicInstorageAmount"));
				
				stockOrderDetail.setTaxPercent(rs.getFloat("TaxPercent"));
				
				stockOrderDetail.setTaxPrice(rs.getFloat("TaxPrice"));
				
				stockOrderDetail.setRemark(rs.getString("Remark"));
				
				
				stockOrderDetails.add(stockOrderDetail) ;
				
			}
			// ps = con.
		} catch (Exception e) {
			// TODO Auto-generated catch block
			throw new Exception(e.getMessage());
		} finally {
			try {
				DBUtil.setNullObject(con, ps, rs);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				throw e;
			}
		}
		return stockOrderDetails;
	}
}
